{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9bd7e494",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第4周\n",
    "* author:邱星倩\n",
    "* time：第4周周二上午\n",
    "* link: [face++](https://console.faceplusplus.com.cn/documents/4887586)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d60ae55d",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "35876899",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "de5a84e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>是否必选</th>\n",
       "      <th>参数名</th>\n",
       "      <th>类型</th>\n",
       "      <th>参数说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_key</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此 API 的 API Key</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>必选</td>\n",
       "      <td>api_secret</td>\n",
       "      <td>String</td>\n",
       "      <td>调用此 API 的 API Secret</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>可选</td>\n",
       "      <td>display_name</td>\n",
       "      <td>String</td>\n",
       "      <td>人脸集合的名字，最长256个字符，不能包括字符^@,&amp;=*'\"</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>可选</td>\n",
       "      <td>outer_id</td>\n",
       "      <td>String</td>\n",
       "      <td>账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>可选</td>\n",
       "      <td>tags</td>\n",
       "      <td>String</td>\n",
       "      <td>FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长255个字符，多个...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>可选</td>\n",
       "      <td>face_tokens</td>\n",
       "      <td>String</td>\n",
       "      <td>人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_t...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>可选</td>\n",
       "      <td>user_data</td>\n",
       "      <td>String</td>\n",
       "      <td>自定义用户信息，不大于16 KB，不能包括字符^@,&amp;=*'\"</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>可选</td>\n",
       "      <td>force_merge</td>\n",
       "      <td>Int</td>\n",
       "      <td>在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_to...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  是否必选           参数名      类型  \\\n",
       "0   必选       api_key  String   \n",
       "1   必选    api_secret  String   \n",
       "2   可选  display_name  String   \n",
       "3   可选      outer_id  String   \n",
       "4   可选          tags  String   \n",
       "5   可选   face_tokens  String   \n",
       "6   可选     user_data  String   \n",
       "7   可选   force_merge     Int   \n",
       "\n",
       "                                                参数说明  \n",
       "0                                  调用此 API 的 API Key  \n",
       "1                               调用此 API 的 API Secret  \n",
       "2                    人脸集合的名字，最长256个字符，不能包括字符^@,&=*'\"  \n",
       "3  账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255...  \n",
       "4  FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长255个字符，多个...  \n",
       "5  人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_t...  \n",
       "6                    自定义用户信息，不大于16 KB，不能包括字符^@,&=*'\"  \n",
       "7  在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_to...  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html(\"\"\"<div class=\"table-wrap\"><table class=\"wrapped confluenceTable\"><colgroup><col><col><col><col></colgroup><tbody><tr><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">是否必选</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数名</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">类型</span></p></th><th class=\"confluenceTh\"><p><span style=\"color: rgb(0,0,0);\">参数说明</span></p></th></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_key</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Key</span></p></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">必选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">api_secret</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">调用此 API 的 API Secret</span></p></td></tr><tr><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">display_name</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">人脸集合的名字，最长<span class=\"inline-comment-marker\" data-ref=\"9f098728-ac30-41a2-aab6-05b79f8a7d2e\">256</span>个字符，不能包括字符^@,&amp;=*'\"</span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">outer_id</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">账号下全局唯一的 FaceSet 自定义标识，可以用来管理 FaceSet 对象。最长255个字符，<span class=\"inline-comment-marker\" data-ref=\"63fc0fd1-cd44-4256-bab5-971a939d66e2\"><span class=\"inline-comment-marker\" data-ref=\"183dcb11-c22c-47f4-aa9c-2ba6f3c8210c\">不能包括字符^@,&amp;=*'\"</span></span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">tags</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">FaceSet 自定义标签组成的字符串，用来对 FaceSet 分组。最长<span class=\"inline-comment-marker\" data-ref=\"1be760b6-f20d-4ff3-a086-b7f763abd84d\">255</span>个字符，多个 tag 用逗号分隔，每个 tag 不能包括字符^@,&amp;=*'\"</span></td></tr><tr><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">可选</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">face_tokens</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">String</span></p></td><td class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">人脸标识 face_token，可以是一个或者多个，用逗号分隔。最多不超过5个 face_token</span></p></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">user_data</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">String</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">自定义用户信息，不大于<span class=\"inline-comment-marker\" data-ref=\"f9edadb1-691d-4d80-933e-909096780f61\">16 KB，不能包括字符^@,&amp;=*'\"</span></span></td></tr><tr><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">可选</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">force_merge</span></td><td colspan=\"1\" class=\"confluenceTd\"><span style=\"color: rgb(0,0,0);\">Int</span></td><td colspan=\"1\" class=\"confluenceTd\"><p><span style=\"color: rgb(0,0,0);\">在传入 outer_id 的情况下，如果 outer_id 已经存在，是否将 face_token 加入已经存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">0：不将 face_tokens 加入已存在的 FaceSet 中，直接返回 FACESET_EXIST 错误</span></p><p><span style=\"color: rgb(0,0,0);\">1：将 face_tokens 加入已存在的 FaceSet 中</span></p><p><span style=\"color: rgb(0,0,0);\">默认值为0</span></p></td></tr></tbody></table></div>\"\"\")[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6de594df",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c4de2f02",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dac09f87",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 准备工作\n",
    "import requests\n",
    "API_Key = \"6DvbqF0lxXKwnYKO2GaZ-CsUEPGPk_ft\"\n",
    "API_secret = \"q1w-oJ-g-tYM_GwkjvqGhBa_i6FsfZL9\"\n",
    "faceset_token =  'a8b0986f2c5307f1d64f6df96d299bda'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a5bd520",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d3c21f9",
   "metadata": {},
   "source": [
    "# 1.创建人脸库"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f13d174",
   "metadata": {},
   "source": [
    "* 创建一个人脸的集合 FaceSet，用于存储人脸标识 face_token。一个 FaceSet 能够存储10000个 face_token。试用API Key可以创建1000个FaceSet，正式API Key可以创建10000个FaceSet。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9c89891a",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "payload = {\n",
    "    'api_key':API_Key,\n",
    "    'api_secret':API_secret\n",
    "}\n",
    "r = requests.post(url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a0173f88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9cbf9562",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'a8b0986f2c5307f1d64f6df96d299bda',\n",
       " 'time_used': 158,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1647917625,76b8ddd1-bcc6-4a6f-a9ca-c6c30dfb4ffa',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19b38ba9",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "158896a0",
   "metadata": {},
   "source": [
    "# 2. 查询人脸库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "d57e7721",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 某一个人脸库\n",
    "url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail'  \n",
    "payload = {\n",
    "    'api_key':API_Key,\n",
    "    'api_secret':API_secret,\n",
    "    'faceset_token':'a8b0986f2c5307f1d64f6df96d299bda'\n",
    "}\n",
    "r = requests.post(url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "48879dad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "5c161591",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'a8b0986f2c5307f1d64f6df96d299bda',\n",
       " 'tags': '',\n",
       " 'time_used': 100,\n",
       " 'user_data': '59人',\n",
       " 'display_name': '20级网新API',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1647919504,7f7bceef-e326-4eee-8b13-cb456e20575b',\n",
       " 'outer_id': 'API032211111'}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68d5731f",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48d586b8",
   "metadata": {},
   "source": [
    "# 3.更新人脸库信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "dc6e49d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/update'\n",
    "payload = {\n",
    "    'api_key':API_Key,\n",
    "    'api_secret':API_secret,\n",
    "    'faceset_token':faceset_token,\n",
    "    'new_outer_id': 'API032211111',\n",
    "    'display_name':'20级网新API',\n",
    "    'user_data':'59人',\n",
    "    'tags':''\n",
    "}\n",
    "r = requests.post(url,params = payload)"
   ]
  },
  {
   "cell_type": "raw",
   "id": "fe6a9624",
   "metadata": {},
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "38b58c66",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'a8b0986f2c5307f1d64f6df96d299bda',\n",
       " 'request_id': '1647919311,b81e0a89-161f-45f7-b112-7c24a96daa7e',\n",
       " 'time_used': 64,\n",
       " 'outer_id': 'API032211111'}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd2e0a96",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f44182c",
   "metadata": {},
   "source": [
    "# 4.获取人脸库列表及信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "08aefa4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 所有人脸库 faceset_token\n",
    "\n",
    "url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets'\n",
    "payload = {\n",
    "    'api_key':API_Key,\n",
    "    'api_secret':API_secret\n",
    "}\n",
    "r = requests.post(url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "eacc89a7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "9dc53762",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 94,\n",
       " 'facesets': [{'faceset_token': '1cba1d6301b413f303eafc3f6d86fa84',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'ca60564c1ed150143690a74312ab6565',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'c028d431fe1099afa1a29825038055ad',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '5ccfb9e5c18c7f83e8dc8ccd324c6ae9',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'cba5a14a31dc915c13728af8377367df',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'a8b0986f2c5307f1d64f6df96d299bda',\n",
       "   'outer_id': 'API032211111',\n",
       "   'display_name': '20级网新API',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1647919766,ce61fec0-b6a3-4cdd-834a-831543b3d627'}"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6277ccf4",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ca34892",
   "metadata": {},
   "source": [
    "# 5. 实现所有人脸功能应用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dceae160",
   "metadata": {},
   "source": [
    "## a. 实现 <人脸检测> 获取face_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c733cb97",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7ac2df75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module detect:\n",
      "\n",
      "NAME\n",
      "    detect\n",
      "\n",
      "FUNCTIONS\n",
      "    face_detect(API_key, API_sercret, image_path)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\thinkpad\\api\\detect.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(detect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "589f5d2a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648185665,754df8e8-2913-4876-9a8f-784a157af1b1',\n",
       " 'time_used': 82,\n",
       " 'faces': [{'face_token': 'f538fa9626cc4986a322038b13090bef',\n",
       "   'face_rectangle': {'top': 85, 'left': 44, 'width': 70, 'height': 70}}],\n",
       " 'image_id': 'hI70bXEW3fZ6NpLe0LeRhQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 图像识别\n",
    "detect.face_detect(API_Key,API_secret,'01.jpg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a2b451d3",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afca9c1b",
   "metadata": {},
   "source": [
    "# 本周作业\n",
    "* 尝试创建 detect.py 并实现功能调用\n",
    "* 尝试创建 analyze.py  compare.py search.py 并实现功能调用\n",
    "\n",
    "* 尝试存放宿舍4位同学的人脸信息到 人脸数据库 （XXX栋XXX宿舍）：\n",
    "> 1.完善faceset信息  \n",
    "> 2.实现人脸搜索和人脸对比，打印结果： 是本人or 不是本人  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2234573d",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "778396dc",
   "metadata": {},
   "source": [
    "## b.实现<人脸分析> analyze.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "565c4cb4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "645288fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module analyze:\n",
      "\n",
      "NAME\n",
      "    analyze\n",
      "\n",
      "FUNCTIONS\n",
      "    face_analyze(API_Key, API_Secret, face_tokens, attributes)\n",
      "        该函数为调用face++ face_analyze接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\thinkpad\\api\\analyze.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(analyze)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c1aaa001",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 134,\n",
       " 'request_id': '1648185679,77861bed-f488-424c-a88c-c66b060b945d',\n",
       " 'faces': [{'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 23},\n",
       "    'smile': {'threshold': 50.0, 'value': 0.346}},\n",
       "   'face_rectangle': {'width': 70, 'top': 85, 'left': 44, 'height': 70},\n",
       "   'face_token': 'f538fa9626cc4986a322038b13090bef'}]}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze.face_analyze(API_Key,API_secret,'f538fa9626cc4986a322038b13090bef',\"gender,age,smiling\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3afa9192",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5d1662fc",
   "metadata": {},
   "source": [
    "## c.实现<人脸对比> compare.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "3254fdb2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "3ddff31a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module compare:\n",
      "\n",
      "NAME\n",
      "    compare\n",
      "\n",
      "FUNCTIONS\n",
      "    face_compare(API_Key, API_Secret, face_token1, face_token2)\n",
      "        face++ 人脸比对\n",
      "\n",
      "FILE\n",
      "    c:\\users\\thinkpad\\api\\compare.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(compare)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "bb6ec12d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648190456,e270c89f-5f9f-4f2e-9d87-8dfcd63ffde8',\n",
       " 'time_used': 87,\n",
       " 'faces': [{'face_token': '02d699b7803f872f0e269db58fdbafa6',\n",
       "   'face_rectangle': {'top': 59, 'left': 51, 'width': 54, 'height': 54}}],\n",
       " 'image_id': '1PuPxuEy2Sbafpcxn6Sdlw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_Key,API_secret,'02.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "ad5714d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648190461,e3444fa0-8bd9-4814-b96f-c45ca7a84503',\n",
       " 'time_used': 306,\n",
       " 'confidence': 46.548,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare.face_compare(API_Key,API_secret,\"35cc1788634e304a6df91d977c8ec366\",\"f538fa9626cc4986a322038b13090bef\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ba00e6b",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "112ba82f",
   "metadata": {},
   "source": [
    "## d.实现<人脸搜索> search.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d43dfd96",
   "metadata": {},
   "outputs": [],
   "source": [
    "import search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9fe81ad4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module search:\n",
      "\n",
      "NAME\n",
      "    search\n",
      "\n",
      "FUNCTIONS\n",
      "    face_search(API_Key, API_Secret, face_token, faceset_token)\n",
      "        该函数为调用face++ face_search接口\n",
      "\n",
      "FILE\n",
      "    c:\\users\\thinkpad\\api\\search.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(search)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ef5370ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648192476,a21f57b6-286d-4a63-8d7b-2b30f78f83d8',\n",
       " 'time_used': 449,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'results': [{'confidence': 31.99,\n",
       "   'user_id': '',\n",
       "   'face_token': '34699e256bf5be7e74a3fd2a48b150a6'}]}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search.face_search(API_Key,API_secret,\"35cc1788634e304a6df91d977c8ec366\",\"5ccfb9e5c18c7f83e8dc8ccd324c6ae9\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26352780",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afeb885a",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d990fe46",
   "metadata": {},
   "source": [
    "# ★★本课实践：宿舍人脸识别门禁系统\n",
    "> 1.创建FaceSet  \n",
    "> 2.查询FaceSet  \n",
    "> 3.存入face_token数据  \n",
    "> 4.获取FaceSet信息数据  \n",
    "> 5.人脸搜索  \n",
    "> 6.人脸对比  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f75f4b0a",
   "metadata": {},
   "source": [
    "### 1.宿舍人员人脸检测 获得其face_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0cbc8bb5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "58b53163",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648193363,2909a6a0-4a47-4160-8d87-348868e21737',\n",
       " 'time_used': 588,\n",
       " 'faces': [{'face_token': '705984bfc451ab6ac92d435735d65609',\n",
       "   'face_rectangle': {'top': 85, 'left': 44, 'width': 70, 'height': 70}}],\n",
       " 'image_id': 'hI70bXEW3fZ6NpLe0LeRhQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_Key,API_secret,'01.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8107bd5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648193382,757b8e44-edcb-436c-9204-2c4acabfd509',\n",
       " 'time_used': 30,\n",
       " 'faces': [{'face_token': '771160dcce18a2937a6beeddd42ab72f',\n",
       "   'face_rectangle': {'top': 59, 'left': 51, 'width': 54, 'height': 54}}],\n",
       " 'image_id': '1PuPxuEy2Sbafpcxn6Sdlw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_Key,API_secret,'02.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f7cc534c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648193383,0c8c43a1-f61e-44f7-ab2d-d1ab9fc492ae',\n",
       " 'time_used': 49,\n",
       " 'faces': [{'face_token': '716e7c9292a6ab4c86dd60bc057d4980',\n",
       "   'face_rectangle': {'top': 61, 'left': 72, 'width': 80, 'height': 80}}],\n",
       " 'image_id': 'nf/eAVhAQxzZm8b25H+hmw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_Key,API_secret,'03.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "c2496854",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648193384,249e3847-a357-4b97-b696-7a13b4ffddd5',\n",
       " 'time_used': 35,\n",
       " 'faces': [{'face_token': '76c0c4780bc253241836878e770ef7ef',\n",
       "   'face_rectangle': {'top': 70, 'left': 65, 'width': 43, 'height': 43}}],\n",
       " 'image_id': 'vCfjQk3BSq/jrPHQ2QAD7Q==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_Key,API_secret,'04.jpg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "23ad1f1b",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d231ba3a",
   "metadata": {},
   "source": [
    "### 2.创建人脸数据库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "5cbed72e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '24d75e96f71caf7217c35d8f28c05f4c',\n",
       " 'time_used': 1578,\n",
       " 'face_count': 4,\n",
       " 'face_added': 4,\n",
       " 'request_id': '1648193476,d69f7f2e-0988-45f7-be64-88f877cad425',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "payload = {\n",
    "    'api_key':API_Key,\n",
    "    'api_secret':API_secret,\n",
    "    'face_tokens':\"705984bfc451ab6ac92d435735d65609,771160dcce18a2937a6beeddd42ab72f,716e7c9292a6ab4c86dd60bc057d4980,76c0c4780bc253241836878e770ef7ef\"\n",
    "}\n",
    "r = requests.post(url,params = payload)\n",
    "r\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75caa785",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dc7fadb9",
   "metadata": {},
   "source": [
    "### 3.更新人脸库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "fb0d19f1",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/update'\n",
    "payload = {\n",
    "    'api_key':API_Key,\n",
    "    'api_secret':API_secret,\n",
    "    'faceset_token':'24d75e96f71caf7217c35d8f28c05f4c',\n",
    "    'new_outer_id': '12-206',\n",
    "    'display_name':'东12栋206房',\n",
    "    'user_data':'4人',\n",
    "    'tags':''\n",
    "}\n",
    "r = requests.post(url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "d1879421",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '24d75e96f71caf7217c35d8f28c05f4c',\n",
       " 'request_id': '1648193704,7fcaa9f9-c555-46e5-8a81-0e477c7b6d3c',\n",
       " 'time_used': 85,\n",
       " 'outer_id': '12-206'}"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f7c1cb6c",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "187a3e57",
   "metadata": {},
   "source": [
    "### 4.查看人脸库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "ddb81d55",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail'  \n",
    "payload = {\n",
    "    'api_key':API_Key,\n",
    "    'api_secret':API_secret,\n",
    "    'faceset_token':'24d75e96f71caf7217c35d8f28c05f4c'\n",
    "}\n",
    "r = requests.post(url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "70b4ef4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '24d75e96f71caf7217c35d8f28c05f4c',\n",
       " 'tags': '',\n",
       " 'time_used': 76,\n",
       " 'user_data': '4人',\n",
       " 'display_name': '东12栋206房',\n",
       " 'face_tokens': ['76c0c4780bc253241836878e770ef7ef',\n",
       "  '705984bfc451ab6ac92d435735d65609',\n",
       "  '716e7c9292a6ab4c86dd60bc057d4980',\n",
       "  '771160dcce18a2937a6beeddd42ab72f'],\n",
       " 'face_count': 4,\n",
       " 'request_id': '1648193796,fba516ec-a0f6-449d-8734-493d6fb9e3d5',\n",
       " 'outer_id': '12-206'}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0567da78",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb31ef31",
   "metadata": {},
   "source": [
    "### 5.人脸检测所要识别的图片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "b0b468fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648195176,1808cb15-1bf8-4bc4-9e03-30d4897541b4',\n",
       " 'time_used': 89,\n",
       " 'faces': [{'face_token': '3d68e1a5b3f90205ca8dcb71750e32d7',\n",
       "   'face_rectangle': {'top': 72, 'left': 60, 'width': 69, 'height': 69}}],\n",
       " 'image_id': 'SAWmBEGnHeFZoXbmC+RwCg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect(API_Key,API_secret,'05.jpg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e61d7fa3",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a51120a",
   "metadata": {},
   "source": [
    "### 6.人脸搜索（找出最为相似的人脸）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "5b0502d6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648216393,3404d567-51d5-4b01-a935-9b0941894818',\n",
       " 'time_used': 383,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'results': [{'confidence': 60.571,\n",
       "   'user_id': '',\n",
       "   'face_token': '771160dcce18a2937a6beeddd42ab72f'}]}"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search.face_search(API_Key,API_secret,\"3d68e1a5b3f90205ca8dcb71750e32d7\",\"24d75e96f71caf7217c35d8f28c05f4c\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7709ce45",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b412cef",
   "metadata": {},
   "source": [
    "### 7.人脸对比（将两张人脸进行比对）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "a2193037",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648216636,61bc0361-4dc9-49b7-b4ac-abc9f32d1d28',\n",
       " 'time_used': 295,\n",
       " 'confidence': 60.571,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "payload = {\n",
    "        \"api_key\":API_Key,\n",
    "        \"api_secret\":API_secret,\n",
    "        \"face_token1\":'3d68e1a5b3f90205ca8dcb71750e32d7',\n",
    "        \"face_token2\":'771160dcce18a2937a6beeddd42ab72f'\n",
    "    }\n",
    "r = requests.post(url,params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "548293ad",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf211560",
   "metadata": {},
   "source": [
    "### 8.打印结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "79d94c4b",
   "metadata": {},
   "outputs": [],
   "source": [
    "face_compare = r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "48ab59d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "confidence = face_compare['confidence']\n",
    "thresholds = face_compare['thresholds']['1e-5']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "66901d8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不是本人\n"
     ]
    }
   ],
   "source": [
    "if confidence > thresholds:\n",
    "    print('是本人')\n",
    "else:\n",
    "    print('不是本人')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b08b9658",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
